package org.NooLab.glue.instances;


import org.NooLab.glue.components.BoardConnectorIntf;
import org.NooLab.glue.components.BoardUpLinkServiceIntf;
import org.NooLab.glue.components.MessageBoardFactory;
import org.NooLab.glue.components.ParticipantsUpLinkConnectorIntf;
import org.NooLab.glue.components.UpLinkConnectorIntf;
import org.NooLab.utilities.logging.PrintLog;



	

public class ReceptorsUpLinkService extends 
											 ParticipantUpLinkServiceAbs 
								  implements 
								  			 UpLinkServiceIntf ,
											 ParticipantUpLinkServiceIntf,
											 BoardUpLinkServiceIntf { 
	// constants ......................
	
	String servedRole = "RECEPTOR" ;
	
	
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	public ReceptorsUpLinkService( ParticipantFactory pFactory, PrintLog outprn){
		super();
		
		
		participantFactory = pFactory ;
		
		factoryProperties = participantFactory.factoryProperties ;
		
		if (outprn!=null){
			out = outprn;
		}
	}

	public ReceptorsUpLinkService( MessageBoardFactory mbFactory, PrintLog outprn){
		super();
		
		mbf = mbFactory;
		factoryProperties = mbFactory.getFactoryProperties() ;
		
		if (outprn!=null){
			out = outprn;
		}
	}
	
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

	
	// ------------------------------------------------------------------------------
	 
	
	@Override
	public void setProcedureAddress( ParticipantsUpLinkConnectorIntf upLinkProcs) {
		upLinkProcedures = upLinkProcs;
		
	}

	public void setBoardLinkerProcAddress( BoardConnectorIntf upLinkProcs ){
		boardLinkProcedures = upLinkProcs;
	}
	
	 
		
	 
	
	@Override
	public void start() {
		
		if (factoryProperties.participantsRole.toLowerCase().contentEquals("board")){
			startInterBoardConnectionChecker();
		}else{
			startClientUplinkChecker();
		}
	}
	
	private void startInterBoardConnectionChecker(){
		
		String role = factoryProperties.participantsRole ;
		started = startUplinkCheckEngine(role);
	}
	
	private void startClientUplinkChecker(){
		
		if (started){
			return;
		}
		
		
		if (participantFactory.xRegistration==null){
			// return;
		}
		
		// MessageBoardParticipant implements ParticipantBaseIntf
		mbp = (MessageBoardParticipant)(participantFactory.getInstance());
		mbf = mbp.getMsgboardFactory() ;
		
		if ((mbp!=null) && (mbf!=null)){
											out.print(3, "starting uplink connection check...");
		
		    started = startUplinkCheckEngine( mbp.getParticipantsRole() );
		}
		
		if(started == false){
			out.printErr(2, "\n>>> starting uplink connection check failed. Server checking remains inactive.");
		}
		
	}


	@Override
	public void stop() {
		
		if (started == false){
			return;
		}
		
		out.print(3, "stopping uplink connection check.");
		super.stop();
	}


	@Override
	public void postPoll(long transactionID, String secret) {
		 
		
	}

	
	// ------------------------------------------------------------------------------

	public String getServedRole() {
		return servedRole;
	}


	public void setServedRole(String servedRole) {
		this.servedRole = servedRole;
	}


	@Override
	public void setPeriodLength(int p) {

		if (p<500){
			p=500 ;
		}
			
		setPeriodLengthBetweenPolls(p) ;
		
	}

 
	
	
}



